package com.imooc.security.user;

import com.lambdaworks.crypto.SCryptUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author : cjf
 * @date : 2021-03-09 14:54
 **/
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository userRepository;

    @Override
    public UserInfo login(UserInfo user) {
        return null;
    }

    @Override
    public UserInfo create(UserInfo info) {
        User user = new User();
        BeanUtils.copyProperties(info,user);
//        user.setUsername(null);
        user.setPassword(SCryptUtil.scrypt(user.getPassword(),16,8,1));
        userRepository.save(user);
        info.setId(user.getId());
        return info;
    }

    @Override
    public UserInfo update() {
        return null;
    }

    @Override
    public void delete(Long id) {

    }

    @Override
    public UserInfo get(Long id) {
        return userRepository.findById(id).get().buildInfo();
    }

    @Override
    public List<UserInfo> findByName(String name) {
        return userRepository.findByName(name);
    }
}
